import { TaskStatusEnum } from '@@/enums/taskEnum'

export function setFlowStatus(res, taskStatus) {
  try {
    const tempData: any = res
    const canvasData = JSON.parse(tempData.canvas)
    if (
      ![TaskStatusEnum.draft, TaskStatusEnum.pendingSubmission, TaskStatusEnum.pendingFeedback].includes(taskStatus)
    ) {
      if (tempData.taskNodeId) {
        if (!tempData.neverWalkedList) {
          tempData.neverWalkedList = []
        }
        tempData.neverWalkedList.push(tempData.taskNodeId)
      }

      canvasData.nodes.forEach((node: any) => {
        if ((tempData.neverWalkedList || []).includes(node.id) && taskStatus !== TaskStatusEnum.completed) {
          node.properties.isFinished = false
        } else {
          node.properties.isFinished = true
        }
        if (tempData.taskNodeId === node.id && taskStatus !== TaskStatusEnum.completed) {
          node.properties.isCurrentNode = true
        }
      })
      ;(tempData.nextNodeInfoVOList || []).forEach((item: any) => {
        const currNode = canvasData.nodes.find((node: any) => node.id === item.nodeId)
        if (currNode) {
          if (item.approvalParamType === 'PRE') {
            currNode.properties['flowable:candidateUsers'] = item.approvalParamValue.join(',')
            currNode.properties['flowable:candidateUsersName'] = item.approvalParamName
          } else {
            currNode.properties['flowable:candidateGroups'] = item.approvalParamValue.join(',')
            currNode.properties['flowable:candidateGroupsName'] = item.approvalParamName
          }
        }
      })
    }
    tempData.canvas = JSON.stringify(canvasData)
    return tempData
  } catch (e) {
    return null
  }
}
